package com.kimwu.design.flyweight.externalstate.factory;

import com.kimwu.design.flyweight.externalstate.product.ExternalIgoChessman;
import com.kimwu.design.flyweight.externalstate.product.impl.ExternalBlackIgoChessman;
import com.kimwu.design.flyweight.externalstate.product.impl.ExternalWhiteIgoChessman;
import com.kimwu.design.flyweight.internalstate.factory.IgoChessmanFactory;

import java.util.Hashtable;

//围棋棋子工厂类：享元工厂类，使用单例模式进行设计
public class ExternalIgoChessmanFactory {
    private static ExternalIgoChessmanFactory instance = new ExternalIgoChessmanFactory();
    private static Hashtable ht; //使用Hashtable来存储享元对象，充当享元池

    private ExternalIgoChessmanFactory() {
        ht = new Hashtable();
        ExternalIgoChessman black, white;
        black = new ExternalBlackIgoChessman();
        ht.put("b", black);
        white = new ExternalWhiteIgoChessman();
        ht.put("w", white);
    }

    //返回享元工厂类的唯一实例  
    public static ExternalIgoChessmanFactory getInstance() {
        return instance;
    }

    //通过key来获取存储在Hashtable中的享元对象  
    public static ExternalIgoChessman getIgoChessman(String color) {
        return (ExternalIgoChessman) ht.get(color);
    }
}  